<?php

namespace mbilling\BillingBundle\Model;

use Doctrine\ORM\EntityManager;
use mbilling\BillingBundle\Entity\messages;


Class MessagesModel
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function findAll()
    {
        $messages = $this->em->getRepository('BillingBundle:messages')->findAll();
        return $messages;
    }

    public function findByPK($id)
    {
        $message = $this->em->find('BillingBundle:messages', $id);
        return $message;
    }

    public function findByUserToId($user_id)
    {
        $message = $this->em->getRepository('BillingBundle:messages')->findBy(array('user_to' => $user_id));
        return $message;
    }

    public function findByWasNotRead($user_id)
    {
        $messages = $this->em->getRepository('BillingBundle:messages')->findBy(array('user_to' => $user_id, 'was_read' => '0'));
        return $messages;
    }
    
    public function create($oEntity)
    {    
        $this->em->persist($oEntity);
        $this->em->flush();
    }
    
    public function removeByPK($id)
    {
        $message = $this->em->find('BillingBundle:messages', $id);
        $this->em->remove($message);
        $this->em->flush();
    }
}
?>
